<template>
  <div class="login-container">
    <div class="login_box">
      <!-- 图标区域 -->
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        class="login_form"
        :model="loginform"
        :rules="loginFromRules"
        ref="loginFormRef"
      >
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="icon-zhanghao iconfont"
            v-model="loginform.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="icon-tianchongxing- iconfont"
            v-model="loginform.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="login_button">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyLogin",
  components: "",
  data() {
    return {
      // 表单数据
      loginform: {
        username: "admin",
        password: "123456",
      },
      loginFromRules: {
        // 验证账号是否合法
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    // 重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 表单预验证
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginform);
        if (res.meta.status != 200)
          return this.$message.error("用户名或密码错误");
        this.$message.success("登录成功");
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style scoped lang="less">
.login-container {
  background-image: linear-gradient(to bottom right, blue, pink);

  height: 100%;
  // 图标样式
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #ccc;
    opacity: 0.8;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    .avatar-box {
      width: 130px;
      height: 130px;
      border-radius: 65px;
      position: absolute;
      margin-left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
      }
    }
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .login_button {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
